package eteam.aps.systemservice.service;

import cn.hutool.core.convert.Convert;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import eteam.aps.common.QueryResult;
import eteam.aps.common.R;
import eteam.aps.common.exceptions.AuthException;
import eteam.aps.systeminterface.model.dto.CustclassDto;
import eteam.aps.systeminterface.model.dto.CustomerDto;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import eteam.aps.systeminterface.model.entity.CustclassEntity;
import eteam.aps.systeminterface.model.entity.CustomerEntity;
import eteam.aps.systeminterface.model.reqparam.IdParam;
import eteam.aps.systeminterface.model.reqparam.QueryCustomerParam;
import eteam.aps.systeminterface.service.ICustController;
import eteam.aps.systemservice.bll.CustomerBll;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 客户资料控制器
 */
@Api(tags = "客户资料控制器")
@RestController
//@RequestMapping(value = "/api/cust")
public class CustController implements ICustController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private CustomerBll customerBll;

    /**
     * 根据父级分类ID获取下级分类列表
     *
     * @param classId: 上级分类ID
     * @return
     */
    @ApiOperationSupport(order = 1, ignoreParameters = {})
    @ApiOperation(value = "根据父级分类ID获取下级分类列表")
    @ApiImplicitParam(name = "classId", value = "分类ID(为空获取顶级分类)")
    @Override
    @GetMapping(value = "/api/cust/getclasslist")
    public R<List<CustclassEntity>> getClassList(@RequestParam String classId) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        return R.success(customerBll.getClassListByPid(tokenDto.getFgrouporgid(), classId));
    }

    /**
     * 新增分类
     *
     * @param custclassDto: 分类信息
     * @return
     */
    @ApiOperationSupport(order = 2, ignoreParameters = {"custclassDto.fclassid"})
    @ApiOperation(value = "新增分类")
    @Override
    @PostMapping(value = "/api/cust/createclass")
    public R createClass(@Validated(Insert.class) @RequestBody CustclassDto custclassDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        customerBll.createCustclass(tokenDto, custclassDto);
        return R.success();
    }

    /**
     * 更新分类
     *
     * @param custclassDto: 分类信息
     * @return
     */
    @ApiOperationSupport(order = 3, ignoreParameters = {})
    @ApiOperation(value = "更新分类")
    @Override
    @PostMapping(value = "/api/cust/updateclass")
    public R updateClass(@Validated(Update.class) @RequestBody CustclassDto custclassDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        customerBll.updateCustclass(tokenDto, custclassDto);
        return R.success();
    }

    /**
     * 删除分类
     *
     * @param idParam: Id参数
     * @return
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "删除分类")
    @Override
    @PostMapping(value = "/api/cust/deleteclass")
    public R deleteClass(@Validated @RequestBody IdParam idParam) throws Exception, AuthException {
        customerBll.deleteCustclass(idParam.getObjid());
        return R.success();
    }


    /**
     * 分页查询客户信息
     *
     * @param requestDto: 查询参数
     * @return
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "分页查询客户信息")
    @ApiImplicitParam(name = "requestDto", value = "分页查询参数")
    @Override
    @GetMapping(value = "/api/cust/querypagelist")
    public R<QueryResult<CustomerEntity>> queryCustPageList(@Validated QueryCustomerParam requestDto) throws Exception, AuthException {
        return R.success(customerBll.queryCustPageList(requestDto));
    }

    /**
     * 新增客户
     *
     * @param customerDto: 客户信息
     * @return
     */
    @ApiOperationSupport(order = 6, ignoreParameters = {"customerDto.fcustid"})
    @ApiOperation(value = "新增客户")
    @Override
    @PostMapping(value = "/api/cust/createcustomer")
    public R createCustomer(@Validated(Insert.class) @RequestBody CustomerDto customerDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        customerBll.createCustomer(tokenDto, customerDto);
        return R.success();
    }

    /**
     * 更新客户
     *
     * @param customerDto: 客户信息
     * @return
     */
    @ApiOperationSupport(order = 7, ignoreParameters = {})
    @ApiOperation(value = "更新客户")
    @Override
    @PostMapping(value = "/api/cust/updatecustomer")
    public R updateCustomer(@Validated(Update.class) @RequestBody CustomerDto customerDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        customerBll.updateCustomer(tokenDto, customerDto);
        return R.success();
    }

    /**
     * 删除客户
     *
     * @param idParam: Id参数
     * @return
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除客户")
    @Override
    @PostMapping(value = "/api/cust/deletecustomer")
    public R deleteCustomer(@Validated @RequestBody IdParam idParam) throws Exception, AuthException {
        customerBll.deleteCustomer(idParam.getObjid());
        return R.success();
    }
}
